package com.tentact.coolmeetingtest.controller.servlet;

import com.tentact.coolmeetingtest.annotation.CookieParam;
import com.tentact.coolmeetingtest.annotation.RequestParam;
import com.tentact.coolmeetingtest.annotation.SessionParam;
import com.tentact.coolmeetingtest.dto.DeptWithEmpList;
import com.tentact.coolmeetingtest.dto.MeetingWithAttendEmps;
import com.tentact.coolmeetingtest.service.MeetingService;
import com.tentact.coolmeetingtest.support.MyMessage;
import com.tentact.coolmeetingtest.util.OtherUtil;
import com.tentact.coolmeetingtest.util.StringUtil;
import com.tentact.coolmeetingtest.vo.Meeting;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 星环
 * @date 2023/2/5 20:55:41
 * @description
 */
public class MeetingServlet {
    MeetingService meetingService = new MeetingService();

    public MyMessage bookMeetingPrev(HttpServlet httpServlet, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println(123213);
        List<DeptWithEmpList> allDeptsWithEmps = meetingService.getAllDeptsWithEmps();
        request.setAttribute("records", allDeptsWithEmps);
        System.out.println("wawawaww");
        return MyMessage.of(false,"bookmeeting.jsp");
    }

    /**
     * 加入@cookieParam  @sessionAttr注解 ，request注入加入新类型：List<String>类型,使用函数式接口 Map<Class<T>,Function<Request,T>>
     * @param request
     * @param meeting
     * @param meeting_emps
     * @return
     * @throws ServletException
     * @throws IOException
     */
    public MyMessage bookMeeting(HttpServletRequest request, Meeting meeting, @RequestParam("meeting_emps") List<String> meeting_emps,
                                 @CookieParam("empno") int bookerNo) throws ServletException, IOException {
        List<Integer> empList = new ArrayList<>();
        for (String emp : meeting_emps) {
            empList.add(StringUtil.parseIntWithNullEmptyCheck(emp));
        }
        meeting.setBookerno(bookerNo);
        System.out.println(meeting);
        int meetingno = meetingService.bookMeeting(meeting);
        meeting.setMeetingno(meetingno);
        try {
            meetingService.addMembers(meetingno, empList);
        } catch (SQLException e) {
            meetingService.deleteOnlyMeeting(meeting);
        }
        return MyMessage.of(true,"room.showAll");
    }

    public MyMessage notificationPrev(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        int userno = Integer.parseInt(OtherUtil.getFromCookies("empno",request.getCookies()));

        //userno got;     need

        List<Meeting> meetingList = meetingService.getMeetingInFuture7DaysByEmpno(userno);
        List<Meeting> offList = new ArrayList<>();
        List<Meeting> onList = new ArrayList<>();
        meetingList.forEach(meeting -> {
            if (meeting.getState() == 0) {
                offList.add(meeting);
            } else if (meeting.getState() == 1) {
                onList.add(meeting);
            }
        });
        System.out.println("这是没有分类的meetings");
        meetingList.forEach(System.out::println);
        offList.forEach(System.out::println);
        System.out.println("这是分过的，上面是关闭的");
        onList.forEach(System.out::println);
        return MyMessage.of(false,"notification.jsp","onList",onList,"offList",offList);
    }

    public void meetingDetailPrev(HttpServlet httpServlet, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        getMeetingDetailPrev(httpServlet, request, response);
        request.getRequestDispatcher("meetingdetail.jsp").forward(request, response);
    }

    public void getMeetingDetailPrev(HttpServlet httpServlet, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String smeetingno = request.getParameter("meetingno");
        int meetingno = StringUtil.parseIntWithNullEmptyCheck(smeetingno);
        MeetingWithAttendEmps record = meetingService.getMeetingDetailByMeetingNo(meetingno);
        System.out.println(record.getMeeting() + "empnum = " + record.getEmpList().size());
        request.setAttribute("record", record);
    }


    public void myBookingsPrev(HttpServlet httpServlet, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //取cookie能不能加到工具类里？
        String sempno = OtherUtil.getFromCookies("empno", request.getCookies());
        int empno = StringUtil.parseIntWithNullEmptyCheck(sempno);
        List<Meeting> meetingList = meetingService.getActiveMeetingByBookerNo(empno);
        System.out.println(meetingList);
        request.setAttribute("meetings", meetingList);

        request.getRequestDispatcher("mybookings.jsp").forward(request, response);

    }

    public void myMeetingDetailsPrev(HttpServlet httpServlet, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        getMeetingDetailPrev(httpServlet, request, response);
        System.out.println(request.getAttribute("record"));
        request.getRequestDispatcher("mybookingdetail.jsp").forward(request, response);
    }

    public void cancelMeeting(HttpServlet httpServlet, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String smeetingno = request.getParameter("meetingno");
        int meetingno = StringUtil.parseIntWithNullEmptyCheck(smeetingno);
        meetingService.cancelMeeting(meetingno);
        myBookingsPrev(httpServlet, request, response);
    }

    public MyMessage showMyMeetings(HttpServletRequest request) throws ServletException, IOException {
        String sempno = OtherUtil.getFromCookies("empno", request.getCookies());
        int empno = StringUtil.parseIntWithNullEmptyCheck(sempno);
        List<Meeting> fullMeetingList = meetingService.getFullDetailedMeetingsByAttemptedEmpno(empno);
        System.out.println(fullMeetingList);
        return MyMessage.of(false,"mymeetings.jsp","meetings",fullMeetingList);
    }
}